﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GraphDrawer.GraphProvider;

namespace GraphDrawer {
	public partial class ComplexNetGenerator : UserControl {
		ArrayOfString parametersTypes;
		
		public ComplexNetGenerator() {
			InitializeComponent();

			GraphProviderServiceSoapClient c = new GraphProviderServiceSoapClient();
            c.GetAvailableMethodsOfGeneratorCompleted +=
				new EventHandler<GetAvailableMethodsOfGeneratorCompletedEventArgs>
					(c_GetAvailableMethodsOfGeneratorCompleted);
            c.GetAvailableMethodsOfGeneratorAsync();
		}

		private void c_GetAvailableMethodsOfGeneratorCompleted(object sender, GetAvailableMethodsOfGeneratorCompletedEventArgs e) {
			ArrayOfString methods = e.Result;

			foreach (string item in methods)
				generators.Items.Add(item);
		}
		
		private void c_GetParametersFromMethodCompleted(object sender, GetParametersFromMethodCompletedEventArgs e) {
			ArrayOfString parameters = e.Result;
			
			foreach (var p in parameters) {
				StackPanel sp = new StackPanel() {
					Orientation = Orientation.Horizontal
				};
				sp.Children.Add(new TextBox() {
					Width = 100
				});
				sp.Children.Add(new TextBlock() {
					Text = "   " + p
				});
				panel.Children.Add(new TextBlock() {
					Text = " "
				});
				panel.Children.Add(sp);
			}
		}

		private void Generators_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			panel.Children.Clear();
			
			GraphProviderServiceSoapClient c = new GraphProviderServiceSoapClient();
			c.GetParametersFromMethodCompleted +=
				new EventHandler<GetParametersFromMethodCompletedEventArgs>
					(c_GetParametersFromMethodCompleted);
			c.GetParametersFromMethodAsync((sender as ComboBox).SelectedItem.ToString());
		}

		private void c_GetParametersTypesFromMethodCompleted(object sender, GetParametersTypesFromMethodCompletedEventArgs e) {
			parametersTypes = new ArrayOfString();
			foreach (var item in e.Result)
				parametersTypes.Add(item);

			string methodName = generators.SelectedItem.ToString();
			ArrayOfAnyType parameters = new ArrayOfAnyType();

			int j = 0;
			for (int i = 1; i < panel.Children.Count; i += 2)
				switch (parametersTypes[j++]) {
					case "System.Int32":
						parameters.Add(int.Parse(((panel.Children[i] as StackPanel).Children[0] as TextBox).Text));
						break;
					case "System.Double":
						parameters.Add(double.Parse(((panel.Children[i] as StackPanel).Children[0] as TextBox).Text));
						break;
					default:
						break;
				}

			GraphProviderServiceSoapClient c = new GraphProviderServiceSoapClient();
			c.GenerateGraphCompleted +=
				new EventHandler<GenerateGraphCompletedEventArgs>
					(c_GenerateGraphCompleted);
			c.GenerateGraphAsync(methodName, parameters);
		}

		private void c_GenerateGraphCompleted(object sender, GenerateGraphCompletedEventArgs e) {
			// TODO add disease model
            this.IsEnabled = true;
            this.Content = new Page(e.Result, "");
            this.Focus();
		}

		private void Generate_Click(object sender, RoutedEventArgs e) {
			if (generators.SelectedItem != null) {
				string methodName = generators.SelectedItem.ToString();

				GraphProviderServiceSoapClient c = new GraphProviderServiceSoapClient();
				c.GetParametersTypesFromMethodCompleted +=
					new EventHandler<GetParametersTypesFromMethodCompletedEventArgs>
						(c_GetParametersTypesFromMethodCompleted);
				c.GetParametersTypesFromMethodAsync(methodName);
			}
		}
	}
}
